/**
 * 整个游戏的图片数据管理
 */

import { __private, Texture2D } from "cc";
import { sampleColorList } from "./Type/Type";

export class Uni8DataMag {
    private static _instance: Uni8DataMag | null = null;


    pixelSize: number = 20;

    samplePatternHeight = sampleColorList.length;
    samplePatternWidth = sampleColorList[0].color.length;
    sampleColorUnit8888: Uint8Array;

    sandTextureWidth = 601;
    sandTextureHeight = 883;
    sandColorUnit8: Uint8Array;


    static getInstance(): Uni8DataMag {
        if (!this._instance) this._instance = new Uni8DataMag();
        return this._instance;
    }
    private constructor() {
        this.init()
    }

    init() {
        this.sandColorUnit8 = this.initSandTexture();
        this.sampleColorUnit8888 = this.initSampleColorBlockData();
    }

    initSandTexture() {
        const uni8Data = new Uint8Array(this.sandTextureWidth * this.sandTextureHeight);
        uni8Data.fill(22);
        // for (let i = 0; i < this.sandTextureWidth * this.sandTextureHeight; i+=this.sandTextureHeight) {
        //     for (let j = 0; j < this.sandTextureHeight && (i + j) < this.sandTextureWidth; j++) {
        //        if (j<100) {
        //             uni8Data[i + j] = 33;
        //        }
        //     }
        // }
        for (let index = 0; index < this.sandTextureHeight ; index++) {
            for (let i = 0; i < this.sandTextureWidth; i++) {
                if (index < 100 && i < 100) {
                     uni8Data[i + index * this.sandTextureWidth] = 33;
                }
            }
        }
        return uni8Data;
    }

    /**
     * 生成采样颜色数据,生成后固定不变
     */
    initSampleColorBlockData() {
        let uni8Width = this.samplePatternWidth * this.pixelSize;
        let uni8Height = this.samplePatternHeight * this.pixelSize;
        let uni8Data = new Uint8Array(uni8Width * uni8Height * 4);
        let num = 0;
        for (let h = 0; h < this.samplePatternHeight; h++) {
            const outlineRGB = sampleColorList[h].outline;
            for (let w = 0; w < this.samplePatternWidth; w++) {
                //取出颜色
                const color = sampleColorList[h].color[w];
                // console.log(color, 'color')
                //算出每个逻辑像素转换到unit8时的大小；
                let startX = w * this.pixelSize;
                let startY = h * this.pixelSize;
                let endX = startX + this.pixelSize;
                let endY = startY + this.pixelSize;
                //填充颜色
                for (let y = startY; y < endY; y++) {
                    for (let x = startX; x < endX; x++) {
                        num += 1;
                        // console.log(num,'采样色块生成总次数')
                        let index = (y * uni8Width + x) * 4;
                        if (y == startY || y == endY || x == startX || x == endX) {
                            uni8Data[index] = outlineRGB.r;
                            uni8Data[index + 1] = outlineRGB.g;
                            uni8Data[index + 2] = outlineRGB.b;
                            uni8Data[index + 3] = outlineRGB.a;
                        } else {
                            uni8Data[index] = color.r;
                            uni8Data[index + 1] = color.g;
                            uni8Data[index + 2] = color.b;
                            uni8Data[index + 3] = color.a;
                        }

                    }
                }

            }
        }
        return uni8Data;
    }
    /**
     * 返回采样颜色块的纹理数据
     */
    assemblySampleTextuteData(): __private._cocos_asset_assets_image_asset__ImageSource {
        return {
            _data: this.sampleColorUnit8888,
            _compressed: false,
            width: this.samplePatternWidth * this.pixelSize,
            height: this.samplePatternHeight * this.pixelSize,
            format: Texture2D.PixelFormat.RGBA8888
        }
    }

    /**
     * 返回沙颜色块的纹理数据
     */
    assemblySandTextuteData(): __private._cocos_asset_assets_image_asset__ImageSource {
        console.log(this.sandColorUnit8, 'this.colorTex')
        return {
            _data: this.sandColorUnit8,
            _compressed: false,
            width: this.sandTextureWidth,
            height: this.sandTextureHeight,
            format: 4
        }
    }
}